import Vue from 'vue'
import Router from 'vue-router'
import Home from './views/Home/Home.vue'
import CourseDetail from './views/Register/Register.vue'
import Login from './views/Login/Login.vue'
import Order from './views/Order/Order.vue'
import Pay from './views/Pay/Pay.vue'
import Personal from './views/Personal/Personal.vue'
import Register from './views/Register/Register.vue'
import VideoList from './views/VideoList/VideoList.vue'
import Dynamic from './views/Dynamic/Dynamic.vue'
import Vaccine from './views/Home/Vaccine.vue'
import Set from './views/Personal/Set.vue'
import Detection from './views/Personal/Detection.vue'
import Chat from './views/Personal/Chat.vue'
import ArticleDetail from './views/Home/articleDetail.vue'
Vue.use(Router)
const routes = [
  {
    path: "/",
    name: "Home",
    component: Home,
    meta: { index: 1 },
  },
  {
    path: "/coursedetail",
    name: "CourseDetail",
    component: CourseDetail,
    meta: { index: 2 },
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
    meta: { index: 3 },
  },
  {
    path: "/order",
    name: "Order",
    component: Order,
    meta: { index: 4 },
  },
  {
    path: "/pay",
    name: "Pay",
    component: Pay,
    meta: { index: 5 },
  },
  {
    path: "/personal",
    name: "Personal",
    component: Personal,
    meta: { index: 6 },
  },
  {
    path: "/register",
    name: "Register",
    component: Register,
    meta: { index: 7 },
  },
  {
    path: "/videolist",
    name: "VideoList",
    component: VideoList,
    meta: { index: 8 },
  },
  {
    path: "/dynamic",
    name: "Dynamic",
    component: Dynamic,
    meta: { index: 9 },
  },
  {
    path: "/vaccine",
    name: "Vaccine",
    component: Vaccine,
    meta: { index: 10 },
  },
  {
    path: "/set",
    name: "Set",
    component: Set,
    meta: { index: 11 },
  },
  {
    path: "/detection",
    name: "Detection",
    component: Detection,
    meta: { index: 12 },
  },
  {
    path: "/chat",
    name: "Chat",
    component: Chat,
    meta: { index: 13 },
  },
  {
    path: "/articleDetail",
    name: "ArticleDetail",
    component: ArticleDetail,
    meta: { index: 14 },
  },
]
const router = new Router({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 挂载路由导航守卫：
router.beforeEach((to, from, next) => {
  // to表示将要访问的路径
  // from表示从哪个路径跳转而来
  // next() 放行 next('/login') 强制跳转
  if (to.path === '/login') return next()
  if (to.path === '/register') return next()
  // 获取token
  // const tokenStr = window.sessionStorage.getItem('token')
  const tokenStr = window.localStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router